/***************************chromiumupdater.cpp*************************/
/*****************************************************************************/
/*******************Browsers Daily Build Updater************************/
/************Copyright (C) 2010-2011  Gianluigi Biancucci*************/
/*****************************************************************************/
/*****This is free software, and you are welcome to redistribute it****/
/*****and / or modify underthe terms of GPLV2 License.**************/
/*****This program comes with ABSOLUTELY NO WARRANTY*****/
/*****************************************************************************/

#include "ChromiumUpdater.h"
#include "singletondownloader.h"
#include "iostream"
#include "QFile"
#include <QMessageBox>
#include <QIcon>
#include <QLabel>
#include <QDir>
#include <QProcess>
#include <QStringList>
#include "chromiumupdater.h"
#include <QVariant>
#include "paths.h"

ChromiumUpdater::ChromiumUpdater(QString arch)

{


    downloader = SingletonDownloader::getInstance();
    QDir dir;
    dir = QDir();
    if(!dir.exists(CHROPATH+"/"))dir.mkdir(CHROPATH+"/");
    check32=new QFile(CHROPATH+"/check32.txt");
    _32="win32";
    this->setArch(arch);
    BName = "Chromium";
    lastError ="No Error";
}




void ChromiumUpdater::setArch(QString arch){
   //Forced to 32 bit version because there are no 64 bit version of Chromium.(For further use)
    currCheckFile=check32;
    currOsType=_32;
}

bool ChromiumUpdater::getLatestVersion(QString * lastVer){

    QByteArray  res;
    if(downloader->syncGet("http://build.chromium.org/f/chromium/snapshots/chromium-rel-xp/LATEST",&res)){
        QString latestVer(res);
        *lastVer=latestVer;
        return true;
    }
    else{
        lastError ="Undefined error when checking the latest version of Chromium.\nCheck your connection settings and try again.";
        return false;
    }

}

bool ChromiumUpdater::getInstalledVersion(QString * installedVer){
    if(!currCheckFile->exists()){
        currCheckFile->open(QIODevice::WriteOnly);
        currCheckFile->close();
    }
    if(currCheckFile->open(QIODevice::ReadOnly)){
        QString oldVer(currCheckFile->readAll());
        currCheckFile->close();
        *installedVer=oldVer;
        return true;
    }
    else {
        lastError="unable to open Chromium settings file ";
        return false;
    }
}

bool ChromiumUpdater::download(QString version, QString * exePath){

    QByteArray chromium;
    if( downloader->syncGet("http://build.chromium.org/f/chromium/snapshots/chromium-rel-xp/"+version+"/mini_installer.exe",&chromium)){
        QFile* chrfile = new QFile(CHROPATH+"/chromium_"+currOsType+"_"+version+".exe");
        chrfile->open(QIODevice::WriteOnly);
        chrfile->write(chromium);
        chrfile->close();
        delete chrfile;
        currCheckFile->open(QIODevice::WriteOnly);
        QVariant v = version;
        currCheckFile->write(v.toByteArray());
        currCheckFile->close();
        *exePath= CHROPATH+"/chromium_"+currOsType+"_"+version+".exe";
        return true;
    } else {
        lastError ="Undefined error when downloading chromium installer.\nCheck your connection settings and try again.";
        return false;
    }
}



QString ChromiumUpdater::getBrowserName(){

    return BName;

}

QString ChromiumUpdater::getLastError(){

    return lastError;

}
